<?php

/**
 * 优惠券管理
 * szk
 * 2022/7/16
 */

namespace App\Http\Controllers\web;

use App\Http\Controllers\Controller;
use App\Http\Requests\CouponSchedulePostRequest;
use App\Http\Requests\DiscountCouponPostRequest;
use App\Repository\RepositoryInterface\CouponInterface;
use HttpResponseException;
use Illuminate\Http\Request;


class DiscountCouponController extends Controller
{
    protected $storeCoupon;

    public function __construct(CouponInterface $storeCoupon)
    {
        $this->storeCoupon = $storeCoupon;
    }

    /**
     * szk
     * 优惠券修改
     * @id 优惠券id
     * @param Request $request
     * @param $id
     * @return array
     */
    public function modification(Request $request, $id)
    {
        $discountData = $request->post();
        return $this->storeCoupon->modification($discountData, $id);
    }

    /**
     * szk
     * 优惠券列表
     * @param Request $request
     * @return array
     */
    public function listview(Request $request)
    {
        $where = [];
        if ($request->get('title') != "null" && $request->get('title') !== null) {
            $title = $request->get('title');
            $where[] = ['title', 'like', "%$title%"];
        }
        if ($request->get('type') != null) {
            $type = $request->get('type');
            $where[] = ['type', '=', "$type"];
        }
        return $this->storeCoupon->listview($where);
    }

    /**
     * szk
     * 优惠券删除
     * @param $id
     * @return array
     */
    public function delete($id)
    {
        return $this->storeCoupon->delete($id);
    }


    /**
     * szk
     * 商品优惠券添加
     * @param DiscountCouponPostRequest $request
     * @return array
     */
    public function create(DiscountCouponPostRequest $request)
    {
        $discountData = $request->validated();
        return $this->storeCoupon->create($discountData);
    }

    /**
     * szk
     * 优惠券详情信息
     * @param $id
     * @return array
     */
    public function read($id)
    {
        return $this->storeCoupon->read($id);
    }

    /**
     * szk
     * 优惠券附表信息添加
     * @param CouponSchedulePostRequest $request
     * @return array
     */
    public function attachedCreate(CouponSchedulePostRequest $request)
    {
        $attachedData = $request->validated();
        if ($attachedData['is_full_give'] !== 0) {
            $attachedData['full_reduction'] = $request->post('full_reduction');
        }
        return $this->storeCoupon->attachedCreate($attachedData);
    }

    /**
     * szk
     * 优惠券附表修改信息
     * @param Request $request
     * @param $id
     * @return array
     */
    public function attachedUpdate(Request $request, $id)
    {
        $attachedData = $request->post();
        return $this->storeCoupon->attachedUpdate($attachedData, $id);
    }

    /**
     * szk
     * 查询个人优惠券
     * @param $id
     * @return array
     */
    public function userList($id)
    {
        return $this->storeCoupon->userList($id);
    }

    /**
     * szk
     * 用户获取优惠券
     * @uid 用户id
     * @couponId 优惠券id
     * @param Request $request
     * @return array
     */
    public function useradd(Request $request)
    {
        $uid = $request->post('uid');
        $couponId = $request->post('couponId');
        return $this->storeCoupon->useradd($uid, $couponId);
    }

    /**
     * 优惠券的状态
     * @param Request $request
     * @return array
     */
    public function changeStatus(Request $request)
    {
        $id = $request->get('id');
        $status = $request->get('status');
        return $this->storeCoupon->changeStatus($id, $status);
    }

    /**
     * 获取商品信息
     * @return mixed
     */
    public function getGoods()
    {
        return $this->storeCoupon->getGoods();
    }

}
